﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Z2.Core
{
	public class Sun : IDraw
	{
		private Sprite m_sun;
		private Sprite m_sunCorona;
		private StaticLight m_sunLight;
		private Vector2 m_position = new Vector2(700, 768);

		public Sun()
		{
			this.m_sun = SpriteManager.Instance.LoadSpriteCache("sun", 1, 1, "world");
			this.m_sunCorona = SpriteManager.Instance.LoadSpriteCache("corona", 1, 1, "world");
			this.m_sun.Depth = Depths.Background + 0.025F;
			this.m_sunCorona.Depth = 0.99F;

			this.m_sunLight = new StaticLight();
			this.m_sunLight.Light = new Light("light");

			this.m_sunLight.Position = this.m_position;
			this.m_sunLight.Size = new Rectangle(0, 0, 128, 128);
			this.m_sunLight.Light.Color = Color.Yellow;
			World.Current.AddLight(this.m_sunLight);
		}

		public void Update()
		{
			this.m_sun.Visibility = World.Current.GlobalLight;
			this.m_sunLight.Light.Color = World.Current.GlobalColorLight;
			float steps = Math.Abs(World.Current.GlobalLight - World.Current.GlobalLightTo) / World.Current.GlobalLightStep;
			if (steps > 0)
			{
				float step = 0;
				if (World.Current.GlobalLightTo > World.Current.GlobalLight)
				{
					step = -((this.m_position.Y - 20F) / steps);
				}
				else
				{
					step = (768F - this.m_position.Y) / steps;
				}
				this.m_position = new Vector2(this.m_position.X, this.m_position.Y + step);
				this.m_sunLight.Position = this.m_position;
			}

			Vector2 v = new Vector2(
				(this.m_sunLight.Position.X - Camera.Instance.Position.X) + (this.m_sunLight.Light.LightSize.Width / 2),
				(this.m_sunLight.Position.Y - Camera.Instance.Position.Y) + (this.m_sunLight.Light.LightSize.Height / 2));
			float updateTo = World.Current.ContainsItem(v) ? 0F : 1F;
			if(this.m_sunCorona.Visibility != updateTo)
				this.m_sunCorona.Visibility += (this.m_sunCorona.Visibility > updateTo)? -0.1F : 0.1F;
		}

		public void Draw(SpriteBatch batch)
		{
			this.m_sun.Draw(batch, this.m_position);
			this.m_sunCorona.Draw(batch, Vector2.Subtract(this.m_position, new Vector2(34F, 34F)));
		}

		public void DrawLight(SpriteBatch batch)
		{
		}

	}
}
